package zhengdaoyang.settings.service.impl;

import zhengdaoyang.settings.dao.UserDao;
import zhengdaoyang.settings.dao.UserDaoImpl;
import zhengdaoyang.settings.domain.User;
import zhengdaoyang.settings.exception.LoginException;
import zhengdaoyang.settings.service.UserService;

import java.util.HashMap;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private final UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String loginAct, String loginPwd) throws LoginException {

        Map<String,String> map = new HashMap<String,String>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);

        int num = userDao.login(map);
        User user = new User();

        if (num ==1){
            user.setUsername(loginAct);
            user.setPassword(loginPwd);
            System.out.println("正确返回到service层");
        }else {
            throw new LoginException("账号密码错误");
        }
        return  user;
    }

    public int findid(String name){
        int kk = 0 ;
        System.out.println("进入到service层");
        try{
            kk = userDao.findid(name);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  kk ;
    }

    @Override
    public int updatePassword(String name, String password) {
        int kk = 0 ;
        try{
            kk = userDao.updatePassword(name,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  kk ;
    }

    @Override
    public int insertuser(String name, String password) {
        int kk = 0 ;
        try{
            kk = userDao.insertuser(name,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (kk!=0){
            return  kk;
        }
        return 0;
    }
}
